<?php

/**
 * Description of MysqlHelper
 *
 * @author michael
 */
class MysqlHelper {

    //put your code here
    public function getMsgs() {
        $conn = mysqli_connect('localhost', 'root', '', 'test', '3306');
        if (!$conn) {
            die('Could not connect to MySQL: ' . mysqli_connect_error());
        }
        mysqli_query($conn, 'SET NAMES \'utf8\'');
// TODO: insert your code here.
        $result = $conn->query("SELECT * FROM message");
        $rows = $result->fetch_assoc();
        mysqli_close($conn);
        return $rows;
    }

    public function insertMsg($msg) {
        $conn = mysqli_connect('localhost', 'root', '', 'test', '3306');
        if (!$conn) {
            die('Could not connect to MySQL: ' . mysqli_connect_error());
        }
        mysqli_query($conn, 'SET NAMES \'utf8\'');
// TODO: insert your code here.
        $sql_insert = "insert into message(msg_from,msg_to,msg,msg_datetime) values("
                . '"' . $msg['msg_from'] . '","'
                . '"' . $msg['msg_to'] . '","'
                . '"' . $msg['msg'] . '","'
                . '"' . $msg['msg_datetime'] . '")';
        $i = $conn->query($sql_insert);
        mysqli_close($conn);
        return $i;
    }

}
